<?php

class KePeerBuilder extends SfPeerBuilder
{

	/**
	 * Adds the doValidate() method.
	 * @param      string &$script The script will be modified in this method.
	 */
	protected function addFunctionDoValidate(&$script)
	{
		$table = $this->getTable();
		$script .= "
	/**
	 * Validates all modified columns of given ".$this->getObjectClassname()." object.
	 * If parameter \$columns is either a single column name or an array of column names
	 * than only those columns are validated.
	 *
	 * NOTICE: This does not apply to primary or foreign keys for now.
	 *
	 * @param      ".$this->getObjectClassname()." \$obj The object to validate.
	 * @param      mixed \$cols Column name or array of column names.
	 *
	 * @return     mixed TRUE if all columns are valid or the error message of the first invalid column.
	 */
	public static function doValidate(".$this->getObjectClassname()." \$obj, \$cols = null)
	{
		\$columns = array();

		if (\$cols) {
			\$dbMap = Propel::getDatabaseMap(".$this->getPeerClassname()."::DATABASE_NAME);
			\$tableMap = \$dbMap->getTable(".$this->getPeerClassname()."::TABLE_NAME);

			if (! is_array(\$cols)) {
				\$cols = array(\$cols);
			}

			foreach (\$cols as \$colName) {
				if (\$tableMap->containsColumn(\$colName)) {
					\$get = 'get' . \$tableMap->getColumn(\$colName)->getPhpName();
					\$columns[\$colName] = \$obj->\$get();
				}
			}
		} else {
";
        $validatorsAdd = array();
		foreach ($table->getColumns() as $col) 
        {
            $validatorsAdd[] = $col->getName();
			//$col = $val->getColumn();
			$size = $col->getSize();
			if($col->isAutoIncrement())continue;
			if ( $col->isNotNull() || ($col->getType() == 'VARCHAR' && $size > 0)) 
            {
    			 $nameCol = $col->getName();
    			 if(strpos($nameCol, '.')!== false)
    			 {
                     $nameCol = explode(".", $nameCol);
                     $nameCol = $nameCol[count($nameCol)-1];
                 }
			
				$script .= "
		if (\$obj->isNew() || \$obj->isColumnModified(".$this->getColumnConstant($col)."))
			\$columns['".strtoupper($nameCol)."'] = \$obj->get".$col->getPhpName()."();
";
			} // if
		} // foreach

		foreach ($table->getValidators() as $val) 
        {
			$col = $val->getColumn();
			if(in_array($col->getName(), $validatorsAdd) )continue;
			if (!$col->isAutoIncrement()) 
            {
    			 $nameCol = $col->getName();
    			 if(strpos($nameCol, '.')!== false)
    			 {
                     $nameCol = explode(".", $nameCol);
                     $nameCol = $nameCol[count($nameCol)-1];
                 }
			
				$script .= "
		if (\$obj->isNew() || \$obj->isColumnModified(".$this->getColumnConstant($col)."))
			\$columns['".strtoupper($nameCol)."'] = \$obj->get".$col->getPhpName()."();
";
			} // if
		} // foreach

		$script .= "
		}
";
	} // end addDoValidate()
	
  protected function addDoValidate(&$script)
  {
      $tmp = '';
      $this->addFunctionDoValidate($tmp);

      //$script .= str_replace("return {$this->basePeerClassname}::doValidate(".$this->getPeerClassname()."::DATABASE_NAME, ".$this->getPeerClassname()."::TABLE_NAME, \$columns);\n",
      $script .=  $tmp."\n".
        "\$res =  ValidatorPeer::doValidate(".$this->getPeerClassname()."::DATABASE_NAME, ".$this->getPeerClassname()."::TABLE_NAME, \$columns, \$obj);\n".
        "    if (\$res !== true) {\n".
        "        foreach (\$res as \$failed) {\n".
        "        \$colName = \$failed->getColumn();\n".
        "            if(strpos(\$colName, '.') === false){\n".
        "             \$colName = self::TABLE_NAME.'.'.\$colName;\n".
        "            }\n".
        "            \$col = ".$this->getPeerClassname()."::translateFieldname(\$colName, BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME);\n".
        "            \$col =  strtolower( self::TABLE_NAME.'{'.\$col.'}');\n".
        "            \$col = str_replace('.', '_', \$col);\n".
        "            ErrorLog::reportarError('errors', \$col, \$failed->getMessage() );\n".
        "        }\n".
        "    }\n\n".
        "    return \$res; \n".
        "    }\n";
  }

}

?>
